import { totalSpecs, specsList ,addSpecs, oneSpecs, changeSpecs} from '../../request/api'
import { successAlert } from '../../utils/alert'
const state = {
    tableData: [],
    total: 0,
    page: 1,
    size: 10,
    form: {
        specsname: "", //商品规格名称
        attrs: "", //商品规格属性值
        status: 1
    },
    attrArr : [{ value: "" }]
}

const mutations = {
    changetotal(state, total) {
        state.total = total
    },
    changetableData(state, arr) {
        state.tableData = arr
    },
    changepage(state, page) {
        state.page = page
    },
    changeform(state, obj) {
        state.form = obj
    },
    clearform(state, obj) {//清空form
        state.form = obj
    },
    changeformattrs(state, a) {
        state.form.attrs = a
    },
    changeattrArr(state,attrArr){
        state.attrArr=attrArr
    }

}

const actions = {
    //总数
    totalActions(context) {
        totalSpecs().then(res => {
            if (res.code == 200) {
                context.commit('changetotal', res.list[0].total)
            }
        })
    },
    //规格列表
    specsListActions(context) {
        let page = context.state.page
        let size = context.state.size
        specsList({ page, size }).then(res => {
            if (res.code === 200) {
                if ((res.list === null || res.list.length === 0) && context.state.page > 1) {
                    let page = context.state.page - 1;
                    context.commit('changepage', page);
                    context.dispatch('specsListActions')
                    return
                }
                context.commit('changetableData', res.list)
            }
        })
    },
    pageAcitons(context, page) {
        context.commit('changepage', page)
    },
    clearActions(context, obj) {//清空form
        context.commit('clearform', obj)
    },
    confirmActions(context, a) {//改变attrs
        context.commit('changeformattrs', a)
    },
    addActions(context) {
        addSpecs(context.state.form).then(res => {
            // console.log(123);
            if (res.code === 200) {
                successAlert(res.msg)
                context.dispatch('totalActions')
                context.dispatch('specsListActions')
            }
        });
    },
    oneActions(context,id){
        oneSpecs({id}).then(res=>{
            if(res.code==200){
                let arr=[]
                res.list[0].attrs.map((item,index)=>{
                    arr[index]={}
                    arr[index].value=item
                    return arr
                })
                // console.log(arr);
                res.list[0].id=id
                res.list[0].attrs=arr
                successAlert(res.msg)
                // console.log(res.list);
                context.commit('changeform',res.list[0])
                context.commit('changeattrArr',res.list[0].attrs)
            }
        })
    },
    attrclearActions(context){
        let attrArr=[{ value: "" }]
        context.commit('changeattrArr',attrArr)
    },
    updateActions(context){
        let attrs = context.state.form.attrs.map(item => item.value).join(",");
        context.commit('changeformattrs',attrs)
        changeSpecs(context.state.form).then(res=>{
            if(res.code==200){
                // console.log(res.list);
                successAlert(res.msg)
                context.dispatch('specsListActions')
            }
        })
    }

}

const getters = {
    total(state) {
        return state.total
    },
    tableData(state) {
        return state.tableData
    },
    size(state) {
        return state.size
    },
    page(state) {
        return state.page
    },
    form(state) {
        return state.form
    },
    attrArr(state){
        return state.attrArr
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}